package com.bnzj.cloud.business.core.web;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import com.bnzj.cloud.business.core.persistence.entity.SysActionLog;
import com.bnzj.cloud.business.core.service.SysActionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bnzj.core.action.ActionType;
import com.bnzj.core.action.annotation.Action;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.CurrentUserService;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.UserInfo;
import com.bnzj.core.webmvc.util.WebMvcUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author Barry
 * @createDate 2020/2/17
 * @description
 */
@Tag(name = "系统操作日志", description = "用户登入记录以及部分操作记录")
@RestController
@RequestMapping("/system/log")
public class SysActionLogController {

    @Autowired
    private CurrentUserService gatewayCurrentUserService;
    @Autowired
    private SysActionLogService sysActionLogService;

    @Operation(summary = "分页查询")
    @Action(descriprion = "操作日志查看",moduleName = "操作日志",operateType = ActionType.QUERY_DATA)
    @GetMapping(value = "/page")
    public ResponseResult<Page<SysActionLog>> getActionLogPage(HttpServletRequest request,
                                                               @Parameter(description = "用户账号") @RequestParam(required = false) String account,
                                                               @Parameter(description = "模块名") @RequestParam(required = false) String moduleName) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "create_time"));
        QueryHelpBean queryHelpBean = SysActionLogService.getQueryHelpBean();
        queryHelpBean.setPageable(pageable);
        //根据工号/姓名/模块名/行为/名称，模糊搜索
        IExpression orCondition1 = CompareExpression.build("account", CompareType.like, account);
        IExpression orCondition3 = CompareExpression.build("module_name", CompareType.like, moduleName);
        queryHelpBean.addWhere(orCondition1, orCondition3);
        Page<SysActionLog> all = sysActionLogService.findAll(queryHelpBean);
        return ResponseResult.success(all);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("/one/{id}")
    public ResponseResult<SysActionLog> getById(@Parameter(name = "id", description = "主键", required = true) @PathVariable("id") Long id) {
        Optional<SysActionLog> optional = sysActionLogService.findById(id);
        return ResponseResult.success(optional.orElse(null));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    public ResponseResult<?> addActionLog(@RequestBody SysActionLog sysActionLog) {
        UserInfo userInfo = gatewayCurrentUserService.getUserInfo();
        EntityUtils.saveBefore(sysActionLog, userInfo.getAccount(), userInfo.getName());
        sysActionLogService.add(sysActionLog);
        return ResponseResult.success();
    }
}
